package cloud.seri.iot.aliyun.service

import cloud.seri.iot.aliyun.service.mapper.ProductRequestMapper
import cloud.seri.iot.common.api.ProductManager
import cloud.seri.iot.common.model.CreateProductDTO
import cloud.seri.iot.common.model.ListProductByTagsDTO
import cloud.seri.iot.common.model.PageRequestDTO
import cloud.seri.iot.common.model.UpdateProductDTO
import com.aliyuncs.DefaultAcsClient
import com.aliyuncs.exceptions.ClientException
import com.aliyuncs.iot.model.v20180120.*
import org.springframework.stereotype.Service

@Service
class ProductManagerService(private val client: DefaultAcsClient, private val requestMapper: ProductRequestMapper) : ProductManager
{

    /**
     * 新建产品
     * @param dto CreateProductDTO
     * @return CreateProductResponse?
     */
    @Throws(ClientException::class)
    override fun createProduct(dto: CreateProductDTO): CreateProductResponse?
    {
        return client.getAcsResponse(requestMapper.getCreateProductRequest(dto))
    }

    /**
     * 查询产品详细信息。
     * @param pk String
     * @return QueryProductResponse?
     */
    @Throws(ClientException::class)
    override fun queryProduct(pk: String): QueryProductResponse?
    {
        return client.getAcsResponse(QueryProductRequest().apply { productKey = pk })
    }

    /**
     * 查询产品列表
     * @param dto PageRequestDTO
     * @return QueryProductListResponse?
     */
    @Throws(ClientException::class)
    override fun queryProductList(dto: PageRequestDTO): QueryProductListResponse?
    {
        return client.getAcsResponse(requestMapper.getQueryProductListRequest(dto))
    }

    /**
     * 修改产品信息
     * @param dto UpdateProductDTO
     * @return UpdateProductResponse?
     */
    @Throws(ClientException::class)
    override fun updateProduct(dto: UpdateProductDTO): UpdateProductResponse?
    {
        return client.getAcsResponse(requestMapper.getUpdateProductRequest(dto))
    }

    /**
     * 创建产品标签
     * @param pk String 产品Key，物联网平台为产品颁发的唯一标识。
     * @param tags List<String> 要创建的标签。标签包括TagKey和TagValue，分别对应标签的key和value。
     * @return CreateProductTagsResponse?
     */
    @Throws(ClientException::class)
    override fun createProductTags(pk: String, tags: List<CreateProductTagsRequest.ProductTag>): CreateProductTagsResponse?
    {
        return client.getAcsResponse(CreateProductTagsRequest().apply {
            productKey = pk
            productTags = tags
        })
    }

    /**
     * 删除指定产品
     * @param pk String
     * @return DeleteProductResponse?
     */
    @Throws(ClientException::class)
    override fun deleteProduct(pk: String): DeleteProductResponse?
    {
        return client.getAcsResponse(DeleteProductRequest().apply { productKey = pk })
    }

    /**
     *
     * @param pk String
     * @param tags List<String>
     * @return DeleteProductTagsResponse?
     */
    @Throws(ClientException::class)
    override fun deleteProductTags(pk: String, tags: List<String>): DeleteProductTagsResponse?
    {
        return client.getAcsResponse(DeleteProductTagsRequest().apply {
            productKey = pk
            productTagKeys = tags
        })
    }

    /**
     *
     * @param tags List<String>
     * @param dto PageRequestDTO
     * @return ListProductByTagsResponse?
     */
    @Throws(ClientException::class)
    override fun listProductByTags(dto: ListProductByTagsDTO): ListProductByTagsResponse?
    {
        return client.getAcsResponse(requestMapper.getListProductByTagsRequest(dto))
    }

    /**
     *
     * @param pk String
     * @return ListProductTagsResponse?
     */
    @Throws(ClientException::class)
    override fun listProductTags(pk: String): ListProductTagsResponse?
    {
        return client.getAcsResponse(ListProductTagsRequest().apply { productKey = pk })
    }

    /**
     *
     * @param pk String
     * @param tags List<String>
     * @return UpdateProductTagsResponse?
     */
    @Throws(ClientException::class)
    override fun updateProductTags(pk: String, tags: List<UpdateProductTagsRequest.ProductTag>): UpdateProductTagsResponse?
    {
        return client.getAcsResponse(UpdateProductTagsRequest().apply {
            productKey = pk
            productTags
        })
    }

}
